package com.zimmur.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.order.Cart;
import com.zimmur.service.order.ICartService;
import com.zimmur.service.order.IWishListService;

@RestController
@RequestMapping("/restful/cart")
public class CartController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CartController.class);
	
	@Resource
	private ICartService cartService;
	@Resource
	private IWishListService wishListService;
	
	@Value("#{spingMvcProperties['System.GuestId']}")
	private String guessUserId;
	
	//加入购物车
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public ReturnJsonEntity addCart(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Map<String, Object> map=new HashMap<>();
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
			long userId = getTokenUserId();
			long goodsId = getLongParam("goodsId");
			long sizeCode = getLongParam("sizeCode");
			int quantity = getIntParam("quantity");
			int isPrepay = getIntParam("isPrepay");
				
			if(cartService.addCart(userId, goodsId, sizeCode, quantity,isPrepay)){
				statusCodeEnum = StatusCodeEnum.CODE000000;
			}
			
			int cartItemNum = cartService.countUserCart(getTokenUserId());
			map.put("cartItemNum", cartItemNum);
			
		}catch(Exception er){
			log.error(er.getMessage());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		entity.setData(map);
		return entity;
	}
	//更新购物车数量
	@RequestMapping(value="/update",method = RequestMethod.POST)
	public ReturnJsonEntity updateCart(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Map<String, Object> map=new HashMap<>();
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
			//long userId = getTokenUserId();
			long cartId = getLongParam("cartId");
			int quantity = getIntParam("quantity");
			if(cartService.updateCart(cartId, quantity)){
				statusCodeEnum = StatusCodeEnum.CODE000000;
			}
			
			int cartItemNum = cartService.countUserCart(getTokenUserId());
			map.put("cartItemNum", cartItemNum);
		}catch(Exception er){
			log.error(er.getMessage());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		entity.setData(map);
		return entity;
	}
	/**
	* <p>Title: MoveInWishList</p>
	* <p>Description: 从购物车移入愿望清单 </p>
	* @return
	 */
	@RequestMapping(value="/moveinwishlist",method = RequestMethod.GET)
	public ReturnJsonEntity moveInWishList(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Map<String, Object> map=new HashMap<>();
		try{
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			long userId = getTokenUserId();
			long cartId = getLongParam("cartId");
			long goodsId = getLongParam("goodsId");
			String resource = getParam("resource");
			int flag = wishListService.MoveInWishList(userId,cartId, goodsId,resource);
			if(flag>0){
				statusCodeEnum = StatusCodeEnum.CODE000000;
			}
		}catch(Exception er){
			log.error(er.getMessage());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		entity.setData(map);
		return entity;
	}
	// 购物车删除
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	public ReturnJsonEntity delCart() {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Map<String, Object> map=new HashMap<>();
		try {
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
//			long userId = getTokenUserId();
			long cartId = getLongParam("cartId");
			if(cartService.delCart(cartId)){
				statusCodeEnum = StatusCodeEnum.CODE000000;
			}
			
			int cartItemNum = cartService.countUserCart(getTokenUserId());
			map.put("cartItemNum", cartItemNum);
		} catch (Exception er) {
			log.error(er.getMessage());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		entity.setData(map);
		return entity;
	}
	// 购物车列表
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ReturnJsonEntity cartList() {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;

		try {
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
			long userId = getTokenUserId();
			List<Cart> list = cartService.getCartList(userId);
			if(!list.isEmpty()){
				entity.setData(list);
			}else{
				entity.setData(new ArrayList<>());
			}
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception er) {
			log.error(er.getMessage());
			entity.setData(new ArrayList<>());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());

		return entity;
	}
}
